package com.macro.mall.service.impl;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dao.SmsFlashPromotionProductRelationDao;
import com.macro.mall.dto.SmsFlashPromotionProduct;
import com.macro.mall.mapper.PmsProductAttributeValueMapper;
import com.macro.mall.mapper.PmsSkuStockMapper;
import com.macro.mall.mapper.SmsFlashPromotionProductRelationMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.SmsFlashPromotionProductRelationService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.util.Date;
import java.util.List;

/**
 * 限时购商品关联管理Service实现类
 * Created by macro on 2018/11/16.
 */
@Service
public class SmsFlashPromotionProductRelationServiceImpl implements SmsFlashPromotionProductRelationService {
    @Autowired
    private SmsFlashPromotionProductRelationMapper relationMapper;
    @Autowired
    private SmsFlashPromotionProductRelationDao relationDao;
    @Autowired
    private PmsSkuStockMapper skuStockMapper;
    @Autowired
    private PmsProductAttributeValueMapper attributeValueMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public int create(List<SmsFlashPromotionProductRelation> relationList) {
        for (SmsFlashPromotionProductRelation relation : relationList) {
            relationMapper.insert(relation);
        }
        return relationList.size();
    }

    @Override
    public void update(Long id, SmsFlashPromotionProductRelation relation) {
        relation.setId(id);
        //根据id查询sms_flash_promotion_product_relation得到活动信息及场次信息
        SmsFlashPromotionProductRelationDto productRelationDto=relationMapper.selectByProductRelation(id);
        if (productRelationDto==null){
           throw new RuntimeException("没有找到对应的活动信息");
        }
        //校验
        checkArguments(productRelationDto);
        //根据relation的skuid及第一步得到的活动id及场次id，作为组合条件，删除sms_flash_promotion_sku_reation
        List<PmsSkuStockDto> skuStockDto = relation.getSkuStockList();
        for (PmsSkuStockDto pmsSkuStockDto : skuStockDto) {
            if (pmsSkuStockDto!=null){
                Long skuId = pmsSkuStockDto.getId();
                relationMapper.deleteSmsFlashPromotionSkuSelation(skuId,productRelationDto.getFlashPromotionId(),productRelationDto.getFlashPromotionSessionId());

            }

        }
        //组装信息，批量保存至sms flash promotion_sku_relation表中
        for (PmsSkuStockDto pmsSkuStockDto : skuStockDto) {
            if (pmsSkuStockDto!=null){
                Long skuId = pmsSkuStockDto.getId();
                relationMapper.addSmsFlashPromotionSkuSelation(skuId,relation,pmsSkuStockDto);
                SmsFlashPromotionSkuRelation skuRelation=relationMapper.selectSmsFlashPromotionSkuRelation(skuId);
                //更新redis的预热数据
                redisTemplate.opsForValue().set("SMS_FLASH_PROMOTION_SKU_RELATION:"+skuRelation.getId(),pmsSkuStockDto.getStock().toString());
            }
        }




    }
    public void checkArguments(SmsFlashPromotionProductRelationDto productRelationDto){
        //获取活动日期年月日
        Date startDate = productRelationDto.getStartDate();
        Date endDate = productRelationDto.getEndDate();
        //获取活动时间时分秒
        Time startTime = productRelationDto.getStartTime();
        Time endTime = productRelationDto.getEndTime();
        //获取当前时间
        Date date = new Date();
        //根据date判断当前日期是否在抢购活动日期范围内
//        if (date.getTime()>=(startTime.getTime()+startDate.getTime())&&date.getTime()<=(endDate.getTime()+endTime.getTime())){
//            throw new RuntimeException("当前活动已经开启");
//        }
        //根据判断状态
        if (productRelationDto.getStatus()==0){
            throw new RuntimeException("当前活动未启用");
        }

    }
    @Override
    public int delete(Long id) {
        return relationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SmsFlashPromotionProductRelation getItem(Long id) {
        return relationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SmsFlashPromotionProduct> list(Long flashPromotionId, Long flashPromotionSessionId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);

        return relationDao.getList(flashPromotionId,flashPromotionSessionId);
    }

    @Override
    public long getCount(Long flashPromotionId, Long flashPromotionSessionId) {
        SmsFlashPromotionProductRelationExample example = new SmsFlashPromotionProductRelationExample();
        example.createCriteria()
                .andFlashPromotionIdEqualTo(flashPromotionId)
                .andFlashPromotionSessionIdEqualTo(flashPromotionSessionId);
        return relationMapper.countByExample(example);
    }

    @Override
    public  PmsSkuStockProductAttr findSku(Long id) {
        PmsSkuStockProductAttr pmsSkuStockProductAttr = new PmsSkuStockProductAttr();
        SmsFlashPromotionProductRelation relation = relationMapper.selectByPrimaryKey(id);
        List<PmsSkuStock> skuStock = skuStockMapper.selectSku(relation.getProductId());
        List<PmsProductAttributeValue> attributeValues=attributeValueMapper.selectAttributeValue(relation.getProductId());

        pmsSkuStockProductAttr.setSkuStockList(skuStock);
        pmsSkuStockProductAttr.setProductAttributeValueList(attributeValues);
        return pmsSkuStockProductAttr;
    }
}
